import {defineStore} from 'pinia'
import request from '@/utils/request'
import {ResApi,Profile} from '@/types/data'
import {setProfile,getProfile,removeProfile} from '@/utils/storage'

const useUserStore  = defineStore('user',{
    state(){
        return {
            profile:getProfile() as Profile
        }
    },

    actions:{
        // 账号登录
        async login(account:string,password:string){
            const res = await request.post<ResApi<Profile>>('/login',{account,password})
            this.profile = res.data.result
            setProfile(this.profile)
        },

        // 获取手机验证码
        async sendMobileMsg(mobile:string){
            await request.get('/login/code',{params:{mobile}})
        },

        // 手机验证码登录
        async mobileLogin(mobile:string,code:string){
            const res = await request.post<ResApi<Profile>>('/login/code',{mobile,code})
            this.profile = res.data.result
            setProfile(this.profile)
        },

        // 退出
        logout(){
            this.profile = {} as Profile
            removeProfile()
        }


    },
    getters:{}
})

export default useUserStore